package enumandlambda;

import java.util.Random;

/**
 * @author shkstart
 * @create 2022-11-28-23:06
 */
public class ThreadDemo12 {
    public static void main(String[] args) {
        //1.创建一个线程分组（女子100米比赛）
        ThreadGroup group = new ThreadGroup("thread-group");
        //2.定义一个公共的任务（现成的任务）
        Runnable runTask = new Runnable() {
            @Override
            public void run() {//业务（任务）
                //生成一个1-3秒的随机数
                int num = (1 + new Random().nextInt(2));
                try {
                    Thread.sleep(num * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread t= Thread.currentThread();

                System.out.println(
                        t.getName()+"选手到达终点：" + num + "s"
                );
            }
        };
    //3.创建线程，选手
        Thread t1 = new Thread(group,runTask);//选手1
        Thread t2 = new Thread(group,runTask);//选手2
        Thread t3 = new Thread(group,runTask);//选手3

//        开跑
        t1.start();
        t2.start();
        t3.start();

        //在所有人到达终点之后宣布成绩
        while(group.activeCount()>0){

        }
        System.out.println("比赛结束");
    }
}
